package com.lineage.data.item_etcitem.wand;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.skill.L1BuffUtil;
import com.lineage.server.model.skill.L1SkillUse;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Wand_Long extends ItemExecutor {
  private static final Log _log = LogFactory.getLog(Wand_Long.class);
  
  private boolean _isCrown;
  
  private boolean _isKnight;
  
  private boolean _isElf;
  
  private boolean _isWizard;
  
  private boolean _isDarkelf;
  
  private boolean _isDragonKnight;
  
  private boolean _isIllusionist;
  
  private int _skillid = 0;
  
  private int _needItem;
  
  private int _needItemCount;
  
  public static ItemExecutor get() {
    return new Wand_Long();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    if (pc == null)
      return; 
    if (item == null)
      return; 
    if (pc.isInvisble() || pc.isInvisDelay()) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(281));
      return;
    } 
    if (pc.hasSkillEffect(9732))
      return; 
    int targetID = data[0];
    int spellsc_x = data[1];
    int spellsc_y = data[2];
    if (targetID == pc.getId() || targetID == 0) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(281));
      return;
    } 
    L1Item _needItemmsg = ItemTable.get().getTemplate(this._needItem);
    if (check(pc)) {
      L1BuffUtil.cancelAbsoluteBarrier(pc);
      L1SkillUse l1skilluse = new L1SkillUse();
      l1skilluse.handleCommands(pc, this._skillid, targetID, spellsc_x, spellsc_y, 0, 0);
      pc.setSkillEffect(9732, 30);
    } else {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(264));
    } 
  }
  
  private boolean check(L1PcInstance pc) {
    try {
      if (pc.isCrown() && this._isCrown)
        return true; 
      if (pc.isKnight() && this._isKnight)
        return true; 
      if (pc.isElf() && this._isElf)
        return true; 
      if (pc.isWizard() && this._isWizard)
        return true; 
      if (pc.isDarkelf() && this._isDarkelf)
        return true; 
      if (pc.isDragonKnight() && this._isDragonKnight)
        return true; 
      if (pc.isIllusionist() && this._isIllusionist)
        return true; 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return false;
  }
  
  private void set_use_type(int use_type) {
    try {
      if (use_type >= 64) {
        use_type -= 64;
        this._isIllusionist = true;
      } 
      if (use_type >= 32) {
        use_type -= 32;
        this._isDragonKnight = true;
      } 
      if (use_type >= 16) {
        use_type -= 16;
        this._isDarkelf = true;
      } 
      if (use_type >= 8) {
        use_type -= 8;
        this._isWizard = true;
      } 
      if (use_type >= 4) {
        use_type -= 4;
        this._isElf = true;
      } 
      if (use_type >= 2) {
        use_type -= 2;
        this._isKnight = true;
      } 
      if (use_type >= 1) {
        use_type--;
        this._isCrown = true;
      } 
      if (use_type > 0)
        _log.error("Wand_Long2 可执行职业设定错误:余数大于0"); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void set_set(String[] set) {
    try {
      this._skillid = Integer.parseInt(set[1]);
    } catch (Exception exception) {}
    try {
      int user_type = Integer.parseInt(set[2]);
      set_use_type(user_type);
    } catch (Exception exception) {}
  }
}
